package cn.project.lemontree.controller;


import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Child;
import cn.project.lemontree.bean.Class;
import cn.project.lemontree.service.ChildService;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/child")
@Api(tags = "儿童信息管理模块")
@CrossOrigin  //跨域访问
public class ChildController {
    @Resource
    private ChildService childService;

    @ApiOperation("分页查询儿童信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="className",value="班级名",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name = "childName",value = "儿童姓名",required = false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult<Map<String, Object>> listpage(int page, int size,
                                                      String childName, String className) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", childService.listpage(page, size, childName, className));
        map.put("count", childService.count(childName, className));
        map.put("page", page);
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除儿童")
    @ApiOperation("删除儿童信息")
    @PostMapping("/remove")
    public CommonResult remove(@RequestParam("child_id") int child_id) {
        return CommonResult.success(childService.removeById(child_id));
    }

    @SystemControllerLog(description = "添加儿童")
    @ApiOperation(value = "添加儿童信息")
    @PostMapping("/save")
    public  CommonResult saveChild(@RequestBody Child child,@RequestParam Integer classId){
        Class aClass = new Class();
        aClass.setClassId(classId);
        child.setAClass(aClass);
        return CommonResult.success(childService.saveChild(child));
    }

    @SystemControllerLog(description = "修改儿童")
    @ApiOperation(value = "修改儿童信息")
    @PostMapping("/update")
    public  CommonResult updateChild(@RequestBody Child child,@RequestParam("classId") Integer classId){
        Class aClass = new Class();
        aClass.setClassId(classId);
        child.setAClass(aClass);
        return CommonResult.success(childService.updateChild(child));
    }
    @SystemControllerLog(description = "修改儿童")
    @ApiOperation(value = "修改儿童信息2")
    @PostMapping("/updatechild")
    public  CommonResult updateChild(@RequestBody Child child){
        return CommonResult.success(childService.updateChild(child));
    }
    @SystemControllerLog(description = "根据邀请码加入班级")
    @ApiOperation(value = "通过邀请码加入班级")
    @PostMapping("/setclass")
    public CommonResult setClass(@RequestBody InvitationCodeChildId invitationCodeChildId){
        return CommonResult.success(childService.setClass(invitationCodeChildId.getInvitationCode(),invitationCodeChildId.getChildId()));
    }
    static class InvitationCodeChildId implements Serializable {
        Integer childId;
        Integer invitationCode;

        public Integer getChildId() {
            return childId;
        }

        public Integer getInvitationCode() {
            return invitationCode;
        }
    }
    static class ChildClassId implements Serializable{
        private Child child;
        private Integer classId;

        public Child getChild() {
            return this.child;
        }

        public Integer getClassId(){
            return this.classId;
        }
    }
    @SystemControllerLog(description = "添加儿童")
    @ApiOperation(value = "添加儿童信息2")
    @PostMapping("/savechild")
    public  CommonResult addChild(@RequestBody ChildClassId childClassId){
        Child child;
        child = childClassId.getChild();
        Class aClass = new Class();
        aClass.setClassId(childClassId.getClassId());
        child.setAClass(aClass);
        return CommonResult.success(childService.saveChild(child));
    }
}
